package online.rainy.dbhomework;

import online.rainy.dbhomework.dao.GradeDao;
import online.rainy.dbhomework.pojo.Grade;
import online.rainy.dbhomework.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "detailServlet", value = "/detail")

public class DetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cid = Integer.parseInt(request.getParameter("cid"));
        response.setContentType("text/html");
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GradeDao mapper = sqlSession.getMapper(GradeDao.class);
        List<Grade> list = mapper.getGrades(cid);
        request.setAttribute("result", list);
        request.setAttribute("course", list.get(0).getCourse().getCname());
        request.getRequestDispatcher("/jsp/detail.jsp").forward(request, response);
    }
}
